package com.sinopec.basemodule.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sinopec.basemodule.common.Logger;
import com.sinopec.basemodule.dialog.TankerDialog;

import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by lwj
 */

public abstract class BaseFragment<T extends BasePresenter> extends Fragment implements BaseView {
    protected final String TAG = this.getClass().getSimpleName();
    public T mPresenter;
    protected BaseActivity mContext;
    protected final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject = PublishSubject.create();

    // 是否已经加载view
    protected boolean isPrepared;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Logger.d("123===", TAG + "   onCreateView");
        View view;
        if (getLayoutId() != 0) {
            view = inflater.inflate(getLayoutId(), container, false);
        } else {
            view = super.onCreateView(inflater, container, savedInstanceState);
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Logger.d("123===", TAG + "   onViewCreated");
        initView(view);
        initEvent();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Logger.d("123===", TAG + "   onActivityCreated");
        super.onActivityCreated(savedInstanceState);
        initPrepare();
    }

    // 是否已经加载view
    private synchronized void initPrepare() {
        //如果hasCallUserVisibleHint为false说明该fragment的父载体不是viewpager，所以可以直接初始化数据
        if (isPrepared) {
            initData();
        } else {
            isPrepared = true;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Logger.d(TAG, "setUserVisibleHint = " + isVisibleToUser);
        if (isVisibleToUser) {
            initPrepare();
        }
    }

    // 第一次用户可见 加载数据
//    protected abstract void onFirstUserVisible();

    // 用户可见 开启动画
    protected void onUserVisible() {
    }

    // 第一次用户不可见
    private void onFirstUserInvisible() {
    }

    // 用户不可见 暂停动画
    protected void onUserInvisible() {
    }

    ;

    protected abstract void initView(View view);

    protected void initEvent() {
    }

    protected void initData() {
    }

    protected abstract int getLayoutId();

    /**
     * 获取全局的context
     */
    @Override
    public void onAttach(Context context) {
        Logger.d("123===", TAG + "   onAttach");
        super.onAttach(context);
        mContext = (BaseActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Logger.d("123===", TAG + "   onCreate");
        super.onCreate(savedInstanceState);
//        mContext = (BaseActivity) getActivity();
    }

    @Override
    public void showProgress() {
        mContext.showProgress();
    }

    @Override
    public void showProgress(String msg) {
        mContext.showProgress(msg);
    }

    @Override
    public void showProgress(boolean cancelable) {
        mContext.showProgress(cancelable);
    }

    @Override
    public void showProgress(String msg, int ImageRes) {
        mContext.showProgress(msg, ImageRes);
    }

    @Override
    public void dismissProgress() {
        mContext.dismissProgress();
    }

    @Override
    public void showMessage(String error) {
        mContext.showMessage(error);
    }

    @Override
    public void showErrorMessage(int code, String msg) {
        mContext.showErrorMessage(code, msg);
    }

    @Override
    public PublishSubject<ActivityLifeCycleEvent> getLifecycleSubject() {
        return lifecycleSubject;
    }

    @Override
    public void onDestroy() {
        Logger.d("123===", TAG + "   onDestroy");
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }

    @Override
    public void onDetach() {
        Logger.d("123===", TAG + "   onDetach");
        super.onDetach();
        if (mContext != null) {
            mContext = null;
        }
    }

    @Override
    public void showAlertDialog(String msg, TankerDialog.OptionListener optionListener) {
        mContext.showAlertDialog(msg, optionListener);
    }

    @Override
    public void showAlertDialog(String msg, int ImageRes, TankerDialog.OptionListener optionListener) {
        mContext.showAlertDialog(msg, ImageRes, optionListener);
    }

    @Override
    public void showAlertDialogNoCancel(String content, String secondContent, int ImageRes, TankerDialog.OptionListener optionListener) {
        mContext.showAlertDialogNoCancel(content, secondContent, ImageRes, optionListener);
    }

    @Override
    public void showAlertDialogNoCancel(String content, String confirmText, TankerDialog.OptionListener optionListener) {
        mContext.showAlertDialogNoCancel(content, confirmText, optionListener);
    }

    @Override
    public void navigationTo(Intent intent) {
        mContext.navigationTo(intent);
    }

    @Override
    public void navigationTo(Class clz) {
        mContext.navigationTo(clz);
    }

    @Override
    public int getContentView() {
        return 0;
    }

    @Override
    public void setCustomContentView(@LayoutRes int contentViewID) {
    }

    @Override
    public void configToolbar(CustomToolbar rToolbar) {
    }

    @Override
    public BaseActivity getContext() {
        return mContext;
    }

    @Override
    public Drawable getmDrawable(@DrawableRes int drawableRes) {
        return getResources().getDrawable(drawableRes);
    }

    @Override
    public int getmColor(int resColor) {
        return getResources().getColor(resColor);
    }

    protected void addDisposable(Disposable disposable) {
        if (disposable == null) {
            return;
        }
        mContext.addDisposable(disposable);
    }

    @Override
    public void onResume() {
        super.onResume();
        Logger.d("123===", TAG + "   onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Logger.d("123===", TAG + "   onPause");
    }

}
